<?php
/* Auto burning SPAM 
----------------------------------
   Version 0.5
   HINA::工程幼稚園 (http://hina.ushiisland.net/blog/hinablue)

   Creator          : Hina, Cain Chen.

   Created at       : 2009.1.14
   Last modified at : 2008.1.15

 General Public License
 http://www.gnu.org/licenses/gpl.html

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

*/

function ABS_autoBurningSPAM() {
	global $service, $database;
	
	// DO NOT RUNNING IN DEBUG MODE
	if ($service['debugmode']) die('DO NOT RUNNING IN DEBUG MODE!');
	
	requireComponent("Eolin.PHP.Core");
	requireModel('blog.comment');
	requireModel('blog.trackback');
	requireModel('common.setting');

	$blogid = getBlogId();
	$anhourpasstime = (!is_null(getServiceSetting('AutoBurningTimeStamp'))) ? intval(getServiceSetting('AutoBurningTimeStamp')) : $_SERVER['REQUEST_TIME'] - 3600;
	setServiceSetting('AutoBurningTimeStamp', $_SERVER['REQUEST_TIME']);
	set_time_limit(0);

	// Get comment and trackback data which isFilter!=0 AND match Filter data
	$sql = "SELECT c.blogid, c.id, c.entry, c.ip FROM tc_Comments AS c INNER JOIN tc_Filters AS f ON f.blogid=c.blogid AND f.type='ip' AND f.pattern=c.ip WHERE c.blogid='{$blogid}' AND c.written >= '{$anhourpasstime}' AND c.isFiltered=0";
	if(DBQuery::queryExistence($sql)) {
		$comments = DBQuery::query($sql);
		while($comment = DBQuery::fetch($comments, 'array')) {
			trashComment($comment['blogid'], $comment['id'], $comment['entry'], null);
			ABS_AddIPFilter($comment['ip']);
		}
	}
	$sql = "SELECT t.id, t.blogid, t.ip FROM {$database['prefix']}Trackbacks AS t INNER JOIN tc_Filters AS f ON f.blogid=t.blogid AND f.type='ip' AND f.pattern=t.ip WHERE t.blogid='{$blogid}' AND t.written >= '{$anhourpasstime}' AND t.isFiltered=0";
	if(DBQuery::queryExistence($sql)) {
		$trackbacks = DBQuery::query($sql);
		while($trackback = DBQuery::fetch($trackbacks, 'array')) {
			trashTrackback($trackback['blogid'], $trackback['id']);
			ABS_AddIPFilter($trackback['ip']);
		}
	}

	$sql = "SELECT c.blogid, c.id, c.entry, c.name FROM tc_Comments AS c INNER JOIN tc_Filters AS f ON f.blogid=c.blogid AND f.type='name' AND f.pattern=c.name WHERE c.blogid='{$blogid}' AND c.written >= '{$anhourpasstime}' AND c.isFiltered=0";
	if(DBQuery::queryExistence($sql)) {
		$comments = DBQuery::query($sql);
		while($comment = DBQuery::fetch($comments, 'array')) {
			trashComment($comment['blogid'], $comment['id'], $comment['entry'], null);
			ABS_AddIPFilter($comment['name']);
		}
	}
	$sql = "SELECT t.id, t.blogid, t.subject FROM {$database['prefix']}Trackbacks AS t INNER JOIN tc_Filters AS f ON f.blogid=t.blogid AND f.type='name' AND f.pattern=t.name WHERE t.blogid='{$blogid}' AND t.written >= '{$anhourpasstime}' AND t.isFiltered=0";
	if(DBQuery::queryExistence($sql)) {
		$trackbacks = DBQuery::query($sql);
		while($trackback = DBQuery::fetch($trackbacks, 'array')) {
			trashTrackback($trackback['blogid'], $trackback['id']);
			ABS_AddIPFilter($trackback['subject']);
		}
	}

	$sql = "SELECT c.blogid, c.id, c.entry, c.homepage FROM tc_Comments AS c INNER JOIN tc_Filters AS f ON f.blogid=c.blogid AND f.type='url' AND f.pattern=c.homepage WHERE c.blogid='{$blogid}' AND c.written >= '{$anhourpasstime}' AND c.isFiltered=0";
	if(DBQuery::queryExistence($sql)) {
		$comments = DBQuery::query($sql);
		while($comment = DBQuery::fetch($comments, 'array')) {
			trashComment($comment['blogid'], $comment['id'], $comment['entry'], null);
			ABS_AddIPFilter($comment['homepage']);
		}
	}
	$sql = "SELECT t.id, t.blogid, t.url FROM {$database['prefix']}Trackbacks AS t INNER JOIN tc_Filters AS f ON f.blogid=t.blogid AND f.type='url' AND f.pattern=t.url WHERE t.blogid='{$blogid}' AND t.written >= '{$anhourpasstime}' AND t.isFiltered=0";
	if(DBQuery::queryExistence($sql)) {
		$trackbacks = DBQuery::query($sql);
		while($trackback = DBQuery::fetch($trackbacks, 'array')) {
			trashTrackback($trackback['blogid'], $trackback['id']);
			ABS_AddIPFilter($trackback['url']);
		}
	}
	
	$comments = DBQuery::query("SELECT blogid, id, entry, homepage FROM {$database['prefix']}Comments WHERE blogid='{$blogid}' AND written >= {$anhourpasstime} AND isFiltered>0");
	while($comment = DBQuery::fetch($comments, 'array')) {
		trashComment($comment['blogid'], $comment['id'], $comment['entry'], null);
	}
	$trackbacks = DBQuery::query("SELECT id, blogid, url FROM {$database['prefix']}Trackbacks WHERE blogid='{$blogid}' AND written >= {$anhourpasstime} AND isFiltered>0");
	while($trackback = DBQuery::fetch($trackbacks, 'array')) {
		trashTrackback($trackback['blogid'], $trackback['id']);
	}
	
	
	unset($filters);
	unset($comments);
	unset($trackbacks);
	
	DBQuery::execute("DELETE FROM {$database['prefix']}Comments WHERE blogid = {$blogid} AND written >= {$anhourpasstime} AND isFiltered > 0");
	DBQuery::execute("DELETE FROM {$database['prefix']}Trackbacks WHERE blogid = {$blogid} AND written >= {$anhourpasstime} AND isFiltered > 0");
	
	return true;
}

function ABS_AddIPFilter($ip) {
	requireComponent('Textcube.Data.Filter');
	$filter = new Filter;
	$filter->type = 'ip';
	$filter->pattern = $ip;
	$result = $filter->add();
	return $result;
}
function ABS_AddUrlFilter($url) {
	requireComponent('Textcube.Data.Filter');
	$filter = new Filter;
	$filter->type = 'url';
	$url = substr(DBQuery::escapeString($url), 0, strpos(str_replace('http://','',DBQuery::escapeString($url)),'/')+8);
	$filter->pattern = $url;
	$result = $filter->add();
	return $result;
}
function ABS_AddSubjectFilter($subject) {
	requireComponent('Textcube.Data.Filter');
	$filter = new Filter;
	$filter->type = 'name';
	$filter->pattern = $subject;
	$result = $filter->add();
	return $result;
}
?>